import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
import numeral from 'numeral';
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0.00')
  }
  return value
};
const daoName = "COMP_GSZ";
const ButtonGroup = Button.Group;
const columns = [
  { title: '导入结果', width: 250, sorter: true, dataIndex: 'DRJG', key: 'DRJG', dispaly: true, filter: true, export: false },
  { title: '导入日期', width: 120, sorter: true, dataIndex: 'NY', key: 'NY', dispaly: true, filter: true, export: false },
  { title: '债券简称', width: 180, sorter: true, dataIndex: "ZJJC", key: 'ZJJC', dispaly: true, filter: true, export: true },
  { title: '客户名称', width: 250, sorter: true, dataIndex: 'KHMC', key: 'KHMC', dispaly: true, filter: true, export: true },
  { title: '现有持仓金额(万)', width: 160, sorter: true, render: data => renderNumber(data.XHCCJE), key: 'XHCCJE', dispaly: true, filter: true, export: true },
  { title: '利润(万)', width: 130, sorter: true, render: data => renderNumber(data.LR), key: 'LR', dispaly: true, filter: true, export: true },
  { title: '经济资本(万)', width: 160, sorter: true, render: data => renderNumber(data.JJZB), key: 'JJZB', dispaly: true, filter: true, export: true },

  { title: '员工1', dataIndex: 'EMNO1', key: 'EMNO1', width: 140, sorter: true, dispaly: true, filter: true, export: true },
  { title: '占比1(%)', dataIndex: 'ZB1', key: 'ZB1', width: 120, sorter: true, dispaly: true, export: true },
  { title: '员工2', dataIndex: 'EMNO2', key: 'EMNO2', width: 140, sorter: true, dispaly: true, filter: true, export: true },
  { title: '占比2(%)', dataIndex: 'ZB2', key: 'ZB2', width: 120, sorter: true, dispaly: true, export: true },
  { title: '员工3', dataIndex: 'EMNO3', key: 'EMNO3', width: 140, sorter: true, dispaly: true, filter: true, export: true },
  { title: '占比3(%)', dataIndex: 'ZB3', key: 'ZB3', width: 120, sorter: true, dispaly: true, export: true },
  { title: '员工4', dataIndex: 'EMNO4', key: 'EMNO4', width: 140, sorter: true, dispaly: true, filter: true, export: true },
  { title: '占比4(%)', dataIndex: 'ZB4', key: 'ZB4', width: 120, sorter: true, dispaly: true, export: true },
  { title: '员工5', dataIndex: 'EMNO5', key: 'EMNO5', width: 140, sorter: true, dispaly: true, filter: true, export: true },
  { title: '占比5(%)', dataIndex: 'ZB5', key: 'ZB5', width: 120, sorter: true, dispaly: true, export: true },

];
const controls = [
  { name: 'NY', required: true, type: 'text', label: '日期', grid: 12 / 24, placeholder: "按YYYYMM格式输入,例如:202201", min: 6, max: 6 },
  { name: "ZJJC", label: "债券简称", type: "text", grid: 12 / 24 },
  { name: "KHMC", label: "客户名称", type: "text", grid: 12 / 24 },
  { name: "XHCCJE", label: "现有持仓金额", type: "text", grid: 12 / 24 },
  { name: "LR", label: "利润(万)", type: "text", grid: 12 / 24 },
  { name: "JJZB", label: "经济资本(万)", type: "text", grid: 12 / 24 },

  { name: 'EMNO1', required: false, type: 'text', label: '员工1', dataType: "string", grid: 12 / 24 },
  { name: 'ZB1', required: false, type: 'number', label: '占比1(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO2', required: false, type: 'text', label: '员工2', dataType: "string", grid: 12 / 24 },
  { name: 'ZB2', required: false, type: 'number', label: '占比2(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO3', required: false, type: 'text', label: '员工3', dataType: "string", grid: 12 / 24 },
  { name: 'ZB3', required: false, type: 'number', label: '占比3(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO4', required: false, type: 'text', label: '员工4', dataType: "string", grid: 12 / 24 },
  { name: 'ZB4', required: false, type: 'number', label: '占比4(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO5', required: false, type: 'text', label: '员工5', dataType: "string", grid: 12 / 24 },
  { name: 'ZB5', required: false, type: 'number', label: '占比5(%)', dataType: "number", grid: 12 / 24 },
  { name: 'DRJG', required: false, ignore: true,type: 'text', label: '导入结果', grid: 10 / 24 },

];
const controlsDtip = [
  { name: 'NY', required: true, type: 'text', label: '日期', grid: 12 / 24, placeholder: "按YYYYMM格式输入,例如:202201", min: 6, max: 6 },
  { name: "ZJJC", label: "债券简称", type: "text", grid: 12 / 24 },
  { name: "KHMC", label: "客户名称", type: "text", grid: 12 / 24 },
  { name: "XHCCJE", label: "现有持仓金额", type: "text", grid: 12 / 24 },
  { name: "LR", label: "利润(万)", type: "text", grid: 12 / 24 },
  { name: "JJZB", label: "经济资本(万)", type: "text", grid: 12 / 24 },
  
  { name: 'EMNO1', required: false, type: 'text', label: '员工1', dataType: "string", grid: 12 / 24 },
  { name: 'ZB1', required: false, type: 'number', label: '占比1(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO2', required: false, type: 'text', label: '员工2', dataType: "string", grid: 12 / 24 },
  { name: 'ZB2', required: false, type: 'number', label: '占比2(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO3', required: false, type: 'text', label: '员工3', dataType: "string", grid: 12 / 24 },
  { name: 'ZB3', required: false, type: 'number', label: '占比3(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO4', required: false, type: 'text', label: '员工4', dataType: "string", grid: 12 / 24 },
  { name: 'ZB4', required: false, type: 'number', label: '占比4(%)', dataType: "number", grid: 12 / 24 },
  { name: 'EMNO5', required: false, type: 'text', label: '员工5', dataType: "string", grid: 12 / 24 },
  { name: 'ZB5', required: false, type: 'number', label: '占比5(%)', dataType: "number", grid: 12 / 24 },
];
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class GszDRForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      visible: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    let sort = {
      field: 'NY',
      order: 'descend'
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20, sorter: sort }
    });
    this.loadOptionData();
  }
  componentWillReceiveProps(nextProps) {
    const { showImport } = this.props
    if (!nextProps.showImport && showImport) {
      this.props.dispatch({
        type: `${daoName}/fetch`,
        payload: { daoName, page: 1, pageSize: 20 }
      });
    }
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showImportHandler() {
    this.props.dispatch({
      type: `${daoName}_DataImport/setDisplay`,
      payload: { display: true },
    });
  };
  DownTmportHandler() {
    let columnHeaders = []
    columns.map((c, j) => {
      if (!c.export) {
        return
      }
      let header = {
        Name: c.key,
        Caption: c.title,
        Width: c.width
      }
      columnHeaders.push(header)
    })
    let form = document.createElement('form');//定义一个form表单
    let whereItems = [];
    whereItems.push({
      FieldName: "1",
      Operator: "<>",
      FieldValues: "1"
    })
    const whereCauses = []
    whereCauses.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    const orderItems = []
    form.style = "display:none";
    form.target = "_blank";
    form.method = "post";
    form.action = "/api/ExportXlsx/" + daoName;
    let input1 = document.createElement("input");
    input1.type = "hidden";
    input1.name = "WhereCauses";
    input1.value = JSON.stringify(whereCauses);
    let input2 = document.createElement("input");
    input2.type = "hidden";
    input2.name = "OrderItems";
    input2.value = JSON.stringify(orderItems)
    let input3 = document.createElement("input");
    input3.type = "hidden";
    input3.name = "ColumnHeaders";
    input3.value = JSON.stringify(columnHeaders);
    let input4 = document.createElement("input");
    input4.type = "hidden";
    input4.name = "PermID";
    input4.value = "" + this.props.permID;
    let input5 = document.createElement("input");
    input5.type = "hidden";
    input5.name = "UserID";
    input5.value = "" + this.props.userID;
    document.body.appendChild(form);//将表单放置在web中
    form.appendChild(input1);
    form.appendChild(input2);
    form.appendChild(input3);
    form.appendChild(input4);
    form.appendChild(input5);
    form.submit();
    document.body.removeChild(form)
  }


  showModal(isAdd) {
    console.log(isAdd)

    let key = null
    if (!isAdd) {
      const len = this.props.selectedRowKeys.length;
      if (len === 0) {
        message.info('请选择要编辑的行，如果选择了多行，则使用最后一次选择！');
        return;
      }
      key = this.props.selectedRowKeys[len - 1]
    }
    this.setState({
      visible: true
    })
  }

  handleOk() {
    this.setState({
      visible: false
    });
  }

  handleCancel() {
    this.setState({
      visible: false
    });
  }


  render() {
    let { showEdit, record, showImport, permID, popedomTypes } = this.props
    const pTypes = popedomTypes[permID]
    let data = <ButtonGroup style={{ zIndex: 0 }}>
      <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
      <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
    </ButtonGroup>
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {data}
          </div>
          <font style={{color: "red" }}>注：利润导入为月利润，不是年累计，请注意小数位数！！！；员工1，员工2，员工3，员工4，员工5可填入工号或姓名</font>
          <DataTable ref="WBTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
          <DataImport title="公司信用债导入" importName="COMP_GSZ" daoName="COMP_GSZ" keys={["ID"]} paramNames={["NY"]} importMode="DeleteAfterInsert" controls={controlsDtip} forbidConvert={true} saveByDao={true} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  const { display } = state[`${daoName}_DataImport`];
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    showImport: display
  }
}
GszDRForm = connect(mapStateToProps)(GszDRForm)
export default GszDRForm;

let render = (props) => React.createElement(GszDRForm, props);
let f = new Factory()
f.set("GszDRForm", render)
